﻿using UnityEngine;
using UnityEditor;
using System.Collections;

public class UploadAssetBundleToCDN
{
    [MenuItem("AssetBundle/UploadAssetBundle_Android    上传", false, 501)]
    public static void UploadAssetBundle_Android()
    {
        // 将刚打出来的资源包放置到ComparisonAssetBundle.m_old_asset_bundle_path中
        CopyNewAssetToOldAsset(Platform.GetPlatformFolder(RuntimePlatform.Android));
    }

    [MenuItem("AssetBundle/UploadAssetBundle_IOS    上传", false, 502)]
    public static void UploadAssetBundle_IOS()
    {
        // 将刚打出来的资源包放置到ComparisonAssetBundle.m_old_asset_bundle_path中
        CopyNewAssetToOldAsset(Platform.GetPlatformFolder(RuntimePlatform.IPhonePlayer));
    }

    [MenuItem("AssetBundle/UploadAssetBundle_Windows    上传", false, 503)]
    public static void UploadAssetBundle_Windows()
    {
        // 将刚打出来的资源包放置到ComparisonAssetBundle.m_old_asset_bundle_path中
        CopyNewAssetToOldAsset(Platform.GetPlatformFolder(RuntimePlatform.WindowsEditor));
    }


    private static void CopyNewAssetToOldAsset(string platform_folder_name)
    {
        var full_zip_file_path = FileTool.PathCombine(AssetBundleConfig.TempAssetBundleDirPath_ZIP,platform_folder_name + CompresionTool.extension_name);
        var new_version = VersionTool.GetNextVersion();

        var content = FileTool.ReadFileContent(full_zip_file_path);
        var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(content);

        var upload_url = FileTool.PathCombine(AssetBundleConfig.CdnUrl,platform_folder_name);
        upload_url = FileTool.PathCombine(upload_url, new_version);

        Debug.Log("把 " + full_zip_file_path + "\n上传到： " + upload_url);
        new WWW(upload_url, bytes);
    }
}
 